#ifndef DOWNLOAD_TASK_H_
#define DOWNLOAD_TASK_H_

#include "cx_system_define.h"
#include "comm_objbase.h"
#include "cx_DownloadOperator.h"

#include "cx_socket.h"
#include "cx_http.h"
#include "cx_file.h"

/*************************************************************************
* INTERFACE
*************************************************************************/

#define		KBreakSize                  (1024*300)

//#undef	THIS_HTTP_CTX
//#define THIS_HTTP_CTX( __m )		_qqr_http_ctx->__m

COMM_INTERFACE_( IDownloadObserver, 1, ICommO )
{
	__COMM_INHERIT_FROM( ICommO )

	COMM_METHOD( NotifyStatus)( COMM_REF(ICommO) thiz,  IN int status );
	COMM_METHOD( NotifyProgress)(COMM_REF(ICommO) thiz, IN int aReceived);
	COMM_METHOD( NotifyTotalProgress)(COMM_REF(ICommO) thiz, IN int aReceived, IN int aTotalSize);
};

#define IComm_cx_DownloadObserver_VTBL( p )		              COMM_GET_VTBL_(p, IDownloadObserver)
#define IComm_cx_DownloadObserver_CALL( p, a )		              IComm_cx_DownloadObserver_VTBL( p )->a
#define IComm_cx_DownloadObserver_NotifyStatus( p, a )           IComm_cx_DownloadObserver_CALL( p, NotifyStatus )( p, a )
#define IComm_cx_DownloadObserver_NotifyProgress( p, a )         IComm_cx_DownloadObserver_CALL( p, NotifyProgress )( p, a )
#define IComm_cx_DownloadObserver_NotifyTotalProgress( p, a ,b)  IComm_cx_DownloadObserver_CALL( p, NotifyTotalProgress )( p, a ,b )


struct __struct_download_task_data
{
	unsigned char*      url;
	unsigned short*     filename;
	unsigned long       offset;
};

struct __struct_download_task_data;
typedef struct __struct_download_task_data    download_task_data_t;
typedef struct __struct_download_task_data *  download_task_data_ptr_t;

typedef struct __struct_comm_download_task_data
{
	unsigned long		      ref_cnt;		
	download_task_data_ptr_t  taskdata;
	COMM_REF(ICommO)	      observer;
	COMM_REF(ICommO)	      downoperator;
	
	qqr_http_context_ptr_t _qqr_http_ctx;	
	CXIAP *iap;
	int                      nIapNumber;
	CXFILE                      m_cxFile;
	int                         m_nTotalLength;
	QBOOL                       m_bStartSaveFile;	
	int                         m_nTryAgainConnect;
	QBOOL						m_bBreak_StartFlag;
	QBOOL                       m_bReConnect;  //ReConnect because package is over big  	
}__struct_comm_download_task_data;

struct __struct_comm_download_task_data;
typedef struct __struct_comm_download_task_data    comm_download_task_data_t;
typedef struct __struct_comm_download_task_data *	comm_download_task_data_ptr_t;


COMM_INTERFACE_( ICxDownTask, 1, IDownloadObserver )
{
	__COMM_INHERIT_FROM( IDownloadObserver )

	COMM_METHOD( UpdateStatus)	    ( COMM_REF(ICommO) thiz, IN int status );
	COMM_METHOD( Start )			( COMM_REF(ICommO) thiz );
	COMM_METHOD( Pause )			( COMM_REF(ICommO) thiz );
	COMM_METHOD( Stop )	        ( COMM_REF(ICommO) thiz );
	COMM_METHOD( CloseDownloadFile) ( COMM_REF(ICommO) thiz );
	COMM_METHOD( SetIap)	    ( COMM_REF(ICommO) thiz, IN int iap );
};

/*************************************************************************
* DECLARATION
*************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif	// #if defined(__cplusplus)

	COMM_IMPORT COMM_REF(ICommO)	comm_cx_create_download_task( void * reserved );

#if defined(__cplusplus)
}
#endif	// #if defined(__cplusplus)

/*************************************************************************
* MACRO
*************************************************************************/

#define IComm_cx_DownTask_VTBL( p )				COMM_GET_VTBL_(p, ICxDownTask)
#define IComm_cx_DownTask_CALL( p, a )				IComm_cx_DownTask_VTBL( p )->a
#define IComm_cx_DownTask_UpdateStatus( p,a )	    IComm_cx_DownTask_CALL( p, UpdateStatus )( p,a )
#define IComm_cx_DownTask_Start( p )		        IComm_cx_DownTask_CALL( p, Start )( p )
#define IComm_cx_DownTask_Pause( p )		        IComm_cx_DownTask_CALL( p, Pause )( p )
#define IComm_cx_DownTask_Stop( p )		        IComm_cx_DownTask_CALL( p, Stop )( p )

#define IComm_cx_DownTask_CloseDownloadFile( p )	IComm_cx_DownTask_CALL( p, CloseDownloadFile )( p )
#define IComm_cx_DownTask_SetIap( p,a )	    IComm_cx_DownTask_CALL( p, SetIap )( p,a )

#endif /* COMM_VIEW_H_ */
